<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>数组的reduce方法</title>
</head>

<body>
  <script>
    // 使用场景: 常常用来数组求和
    // 语法:arr.reduce(function(上一次值,当前值){},初始值)
    // 初始值选填

    // 没有初始值
    const arr = [1, 2, 3]
    const total = arr.reduce((prev, current) => {
      return prev + current
    })
    console.log(total);

    // 有初始值
    const total1 = arr.reduce((prev, current) => prev + current, 10)
    console.log(total1);

    // 计算工资案例
    const money = [
      { name: '张三', salary: 1000 },
      { name: '李四', salary: 2000 },
      { name: '王五', salary: 3000 }
    ]
    const sum = money.reduce((prev, current) => prev + current.salary, 0)
    console.log(sum);
    // 涨薪30%
    const sum1 = money.reduce((prev, current) => prev + current.salary * 1.3, 0)
    console.log(sum1);
  </script>
</body>

</html>